<?php
/*
Copyright 2008, Nathalia


Author:
Nathalia Sautchuk Patrício          (nathalia.sautchuk@gmail.com)

*/
class Editar extends TPage
{
	public function onInit($param)
	{
		parent::onInit($param);

        $this->instituicaoList->DataTextField='Nome';
		$this->instituicaoList->DataValueField='InstituicaoID';
		$this->instituicaoList->DataSource=InstituicaoRecord::finder()->findAll();
		$this->instituicaoList->dataBind();    

		if(!$this->IsPostBack)  // if the page is initially requested
		{
			// Retrieves the existing user data. This is equivalent to:
			// $userRecord=$this->getUserRecord();
			$userRecord=$this->UserRecord;

            $this->DataNascimento->Text = $userRecord->DataNascimento; 
            $this->Nome->Text           = $userRecord->Nome; 
            $this->Login->Text          = $userRecord->Login; 
            $this->Email->Text          = $userRecord->Email; 
			if($this->User->IsAdmin)
                $this->PapelAdmin->SelectedValue = $userRecord->Papel;
			else if($this->User->IsCP)
                $this->PapelCP->SelectedValue = $userRecord->Papel;
            $this->CPF->Text            = $userRecord->CPF; 
            $this->Email->Text          = $userRecord->Email; 
            $this->Endereco->Text       = $userRecord->Endereco; 
            $this->CEP->Text            = $userRecord->CEP; 
            $this->Cidade->Text         = $userRecord->Cidade; 
            $this->Estado->Text         = $userRecord->Estado; 
            $this->Pais->Text           = $userRecord->Pais; 
            $this->Telefone->Text       = $userRecord->Telefone; 
            $this->instituicaoList->setSelectedValue($userRecord->InstituicaoID);      
		}
	}

	public function editarUsuario($sender,$param)
	{
		if($this->IsValid)  // when all validations succeed
		{
            $userRecord                 = $this->UserRecord;
            $userRecord->DataNascimento = $this->DataNascimento->Text;    
            $userRecord->Nome           = $this->Nome->Text;
            $userRecord->Login          = $this->Login->Text;
            if(!empty($this->Senha->Text))
                $userRecord->Senha      = md5($this->Senha->Text);
            $userRecord->Email          = $this->Email->Text;
			if($this->User->IsAdmin)
				$userRecord->Papel      = (int)$this->PapelAdmin->SelectedValue;
			else if($this->User->IsCP)
				$userRecord->Papel      = (int)$this->PapelCP->SelectedValue;
            $userRecord->CPF            = $this->CPF->Text;
            $userRecord->Email          = $this->Email->Text;
            $userRecord->Endereco       = $this->Endereco->Text;
            $userRecord->CEP            = $this->CEP->Text;
            $userRecord->Cidade         = $this->Cidade->Text;
            $userRecord->Estado         = $this->Estado->Text;
            $userRecord->Pais           = $this->Pais->Text;
            $userRecord->Telefone       = $this->Telefone->Text;
            $userRecord->InstituicaoID  = $this->instituicaoList->getSelectedValue();        

			// saves to the database via Active Record mechanism
			$userRecord->save();

			// redirects the browser to the homepage
			$this->Response->redirect("./index.php?page=Usuarios.Listar");
		}
	}

	protected function getUserRecord()
	{
		// if the 'username' GET var is not empty and the current user
		// is an administrator, we use the GET var value instead.
		if($this->Request['usuarioID']!==null)
			$usuarioID = $this->Request['usuarioID'];
		if($this->Request['usuarioID']==null)
			$usuarioID = $this->User->Name;

		// use Active Record to look for the specified username
		$usuarioRecord = UserRecord::finder()->findByUsuarioID($usuarioID);
		if(!($usuarioRecord instanceof UserRecord))
			throw new THttpException(500,'UsuarioID is invalid.');
		return $usuarioRecord;
	}


}
?>
